package com.connector.base.utils;

import com.google.common.base.Strings;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Utils
{
    public static Map<String, String> headerFromString(final String headersString) {
        if (Strings.isNullOrEmpty(headersString)) {
            return null;
        }
        final Map<String, String> headers = new HashMap<String, String>();
        final int L = headersString.length();
        String value = null;
        int i = 0;
        int start = 0;
        while (i < L) {
            final char c = headersString.charAt(i);
            if (c == '\n') {
                if (start >= L - 1) {
                    break;
                }
                final String header = headersString.substring(start, i);
                start = i + 1;
                final int index = header.indexOf(58);
                if (index > 0) {
                    final String name = header.substring(0, index);
                    if (index < header.length() - 1) {
                        value = header.substring(index + 1);
                    }
                    headers.put(name, value);
                }
            }
            ++i;
        }
        return headers;
    }

    public static String headerToString(final Map<String, String> headers) {
        if (headers != null && headers.size() > 0) {
            final StringBuilder sb = new StringBuilder(headers.size() * 64);
            for (final Map.Entry<String, String> entry : headers.entrySet()) {
                sb.append(entry.getKey()).append(':').append(entry.getValue()).append('\n');
            }
            return sb.toString();
        }
        return null;
    }

    public static String formatDateTime(final String time) {
        final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if (time == null || "".equals(time)) {
            return "";
        }
        Date date = null;
        try {
            date = format.parse(time);
        }catch (ParseException e) {
            e.printStackTrace();
        }
        final Calendar current = Calendar.getInstance();
        final Calendar today = Calendar.getInstance();
        today.set(1, current.get(1));
        today.set(2, current.get(2));
        today.set(5, current.get(5));
        today.set(11, 0);
        today.set(12, 0);
        today.set(13, 0);
        final Calendar yesterday = Calendar.getInstance();
        yesterday.set(1, current.get(1));
        yesterday.set(2, current.get(2));
        yesterday.set(5, current.get(5) - 1);
        yesterday.set(11, 0);
        yesterday.set(12, 0);
        yesterday.set(13, 0);
        current.setTime(date);
        if (current.after(today)) {
            final GregorianCalendar ca = new GregorianCalendar();
            ca.setTime(date);
            return (ca.get(9) == 1) ? "下午" : "上午";
        }
        if (current.before(today) && current.after(yesterday)) {
            return "昨天";
        }
        return "更早";
    }
}
